/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of linkedup
**
** linkedup is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** linkedup is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/

import QtQuick 1.1
import com.meego 1.0

/**
  Standard application page with network support.
  The network requests will also set the 'busy' bool
  to true when they start and false when they end.
  */
Page {
	id: root

	property bool busy
	property alias title: titleText.text
	property int borderHeight: border.height

	signal getFinished(string responseText, string statusCode, string statusText)
	signal postFinished(string responseText, string statusCode, string statusText)
	signal deleteFinished(string responseText, string statusCode, string statusText)

	BusyIndicator{
		z: 100
		running: busy
		visible: running
		anchors.centerIn: parent
		platformStyle: BusyIndicatorStyle{size: "large"}
	}

	Rectangle{
		id: border
		height: 70
  gradient: Gradient {
	  GradientStop {
		  position: 0
		  color: "#83898f"
	  }

	  GradientStop {
		  position: 1
		  color: "#494949"
	  }
  }
		width: root.width
		z: 1
		Label{
			id: titleText
			anchors.left: parent.left
			anchors.margins: 20
			anchors.verticalCenter: parent.verticalCenter
			font.pixelSize: 40
			color: "white"
		}
	}

	function get(url){
		root.busy = true
		var xmlHttp = new XMLHttpRequest();
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				root.busy = false
				root.getFinished(xmlHttp.responseText, xmlHttp.status, xmlHttp.statusText)
				console.log("\n\n"+xmlHttp.status)
				console.log(xmlHttp.statusText)
				console.log(xmlHttp.responseText)
			}
		}
		xmlHttp.open( "GET", url, true );
		xmlHttp.send( null );
	}

	function httpDelete(url){
		root.busy = true
		var xmlHttp = new XMLHttpRequest();
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				root.busy = false
				root.deleteFinished(xmlHttp.responseText, xmlHttp.status, xmlHttp.statusText)
			}
		}
		xmlHttp.open( "DELETE", url );
		xmlHttp.send( null );
	}

	function post(list){
		root.busy = true
		var xmlHttp = new XMLHttpRequest();
		var url = list[0]
		var header = list[1]
		var body = list[2]

		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				root.busy = false
				root.postFinished(xmlHttp.responseText, xmlHttp.status, xmlHttp.statusText)
			}
		}
		xmlHttp.open( "POST", url, true );
		xmlHttp.setRequestHeader("Content-Type", "text/xml")
		xmlHttp.setRequestHeader("Host", "api.linkedin.com");
		xmlHttp.setRequestHeader("Authorization", header);
		xmlHttp.send( body );
	}

}
